package com.taotao.portal.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.taotao.portal.pojo.SearchResult;
import com.taotao.portal.service.SearchService;

@Controller
public class SearchController {

	@Autowired
	private SearchService searchService;

	@RequestMapping("/search")
	public String search(@RequestParam(value="q") String query,
			@RequestParam(value="page",defaultValue = "1") Integer currentPage,
			@RequestParam(defaultValue = "60") Integer rows, Model model)
			throws Exception {

		query = new String(query.getBytes("iso8859-1"), "utf-8");

		SearchResult searchResult = searchService.search(query, currentPage,
				rows);
		// 把数据返回给页面
		model.addAttribute("itemList", searchResult.getItemList());
		model.addAttribute("query", query);
		model.addAttribute("page", currentPage);
		model.addAttribute("totalPages", searchResult.getPageCount());
		model.addAttribute("rows", rows);
		// 返回逻辑视图名
		return "search";
	}
}
